<template>
  <div>
    <TablePageContainer>
      <template #header>
        <el-form :model="table.params" :inline="true">
          <el-form-item label="角色名称">
            <el-input v-model="table.params.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getList">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #operation>
        <el-button type="primary" @click="handleCloneSaveOrEditDialog(true)">新增</el-button>
        <el-button type="danger" :disabled="table.selectionRows.length <= 0" @click="handleDel">批量删除</el-button>
      </template>
      <template #table>
        <el-table v-loading="table.loading" class="w_100" :data="table.data" border stripe @selection-change="handleTableSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="name" label="角色名称" align="center" />
          <el-table-column label="角色描述" width="280" align="center">
            <template slot-scope="scope">
              <el-tooltip placement="top">
                <div slot="content">{{ scope.row.description }}</div>
                <span>{{ `${nullToStr(scope.row.description).substring(0, 15)}${nullToStr(scope.row.description).length > 15 ? '...' : ''}` }}</span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="createdTime" label="创建时间" width="160" align="center" sortable />
          <el-table-column prop="updatedTime" label="修改时间" width="160" align="center" sortable />
          <el-table-column label="操作" width="250" align="center">
            <template slot-scope="scope">
              <el-button type="text" icon="fa-solid fa-stamp" @click="handleRoleAllowDialog(scope.row)">授权</el-button>
              <el-button v-if="scope.row.isAdministrator === 2" type="text" icon="fa fa-pen-to-square" @click="handleEdit(scope.row)">修改</el-button>
              <el-button v-if="scope.row.isAdministrator === 2" type="text" icon="fa fa-trash-can" @click="handleDel(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #page>
        <Pagination :total.sync="table.total" :page.sync="table.params.pageNum" :limit.sync="table.params.pageSize" @pagination="getList" />
      </template>
    </TablePageContainer>

    <SaveOrEdit v-if="saveOrEditDialog.visible" :id="saveOrEditDialog.id" :visible.sync="saveOrEditDialog.visible" :type="saveOrEditDialog.type" @getList="getList" @handleClose="handleCloneSaveOrEditDialog" />

    <RoleAllow v-if="roleAllowDialog.visible" :id="roleAllowDialog.id" :visible.sync="roleAllowDialog.visible" @handleClose="handleCloneRoleAllowDialog" />
  </div>
</template>
<script>
import { del, getPage } from '@/api/role'
import _ from 'lodash'
import { nullToStr } from '@/utils'

export default {
  name: 'Role',
  components: {
    TablePageContainer: () => import('@/components/TablePageContainer/index.vue'),
    Pagination: () => import('@/components/Pagination/index.vue'),
    SaveOrEdit: () => import('@/views/role/components/SaveOrEdit.vue'),
    RoleAllow: () => import('@/views/role/components/RoleAllow.vue')
  },
  data() {
    return {
      table: {
        params: {
          name: '',
          pageNum: 1,
          pageSize: 10
        },
        defaultParams: undefined,
        loading: false,
        data: [],
        total: 0,
        selectionRows: []
      },
      saveOrEditDialog: {
        visible: false,
        type: undefined,
        id: undefined
      },
      roleAllowDialog: {
        visible: false,
        id: undefined
      }
    }
  },
  created() {
    this.table.defaultParams = _.cloneDeep(this.table.params)
    this.getList()
  },
  methods: {
    nullToStr,
    getList() {
      this.table.loading = true
      getPage(this.table.params).then(res => {
        this.table.data = res.data.records
        this.table.total = res.data.total
        this.table.loading = false
      })
    },
    handleTableSelectionChange(val) {
      this.table.selectionRows = val
    },
    handleEdit(row) {
      this.handleCloneSaveOrEditDialog(true)
      this.saveOrEditDialog.type = 'edit'
      this.saveOrEditDialog.id = row.id
    },
    handleDel(row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        if (this.table.selectionRows.length > 0) {
          this.table.selectionRows.forEach(item => {
            if (item.isAdministrator === 1) {
              this.$message.warning('存在超级管理员角色, 不允许删除')
              return
            }
            ids.push(item.id)
          })
        }

        if (row.id) {
          ids.push(row.id)
        }

        if (ids.length <= 0) {
          return
        }

        del(ids).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    handleCloneSaveOrEditDialog(v) {
      this.saveOrEditDialog.type = undefined
      this.saveOrEditDialog.id = undefined
      this.saveOrEditDialog.visible = v
    },
    handleRoleAllowDialog(row) {
      this.roleAllowDialog.id = row.id
      this.roleAllowDialog.visible = true
    },
    handleCloneRoleAllowDialog(v) {
      this.roleAllowDialog.id = undefined
      this.roleAllowDialog.visible = v
    },
    handleReset() {
      this.table.params = _.cloneDeep(this.table.defaultParams)
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
